
#include "FileSave.h"


void  FileSave::savePlayer(const string& i){
string filePath=userPath+userName+i+"_base.txt";
createFolder(filePath);
// 打开文件，如果文件不存在则创建
ofstream outFile(filePath);
if (!outFile) {
    cerr << "无法打开文件: " << filePath << endl;
    return;
}


// 将玩家信息写入一行，使用空格分隔
outFile << p->GetcritChance() << " "
        << p->GetdodgeChance() << " "
        << p->GetName() << " "
        << p->GetSymbol() << " "
        << p->GetAttack() << " "
        << p->GetHP() << " "
        << p->GetDefind() << " "
        << p->Getpoint().Getx() << " "
        << p->Getpoint().Gety() << " "
        << p->GetGloden() << " "
        << p->GetExp() << " "
        << p->GetLevel() << " "
        << p->GetAttrPoint()<<"\n";
        outFile.close(); // 关闭文件
}

//存背包
void  FileSave::saveBag(const string& i){
string filePath=bagPath+userName+i+"_bag.txt";
createFolder(filePath);
// 打开文件，如果文件不存在则创建
ofstream outFile(filePath);
if (!outFile) {
    cerr << "无法打开文件: " << filePath << endl;
    return;
}

auto bag =p->Getbag();
if(bag.size()>0)
{
int i;
for(i=0;i<bag.size();i++)
{   auto ptr= bag[i];
    outFile<<ptr->GetName()<<" "
    <<ptr->Getattribute()<<"\n";

}
}
outFile.close(); // 关闭文件
}




void createFolder(const string& filePath)
{
    std::filesystem::path dirPath = std::filesystem::path(filePath).parent_path();
    // 检查目录是否存在，如果不存在则创建
    if (!std::filesystem::exists(dirPath))
    {
        std::filesystem::create_directories(dirPath);
    }
}



void  FileSave:: saveMonster(const string& a){
string filePath=monsterPath+userName+a+"_monster.txt";
std::vector<Monster> *Monsters=s->getMonsters();
createFolder(filePath);// 获取目录路径
ofstream outFile(filePath);// 打开文件，如果文件不存在则创建
if (!outFile) {
    cerr << "无法打开文件: " << filePath << endl;
    return;
}
int i;
for(i=0;i<Monsters->size();i++){
Monster *p=&(*Monsters)[i];
// 将玩家信息写入一行，使用空格分隔
outFile << p->GetdodgeChance() << " "
        << p->GetcritChance() << " "
        << p->GetName() << " "
        << p->GetHP() << " "
        << p->GetAttack() << " "
        << p->GetDefind() << " "
        << p->GetSymbol() << " "
        << p->GetGloden() << " "
        << p->Getpoint().Getx() << " "
        << p->Getpoint().Gety() << " "
        <<p->GetExp()<<"\n";
}
outFile.close(); // 关闭文件
}


void  FileSave:: saveTreasure(const string& a){
string filePath=treasurePath+userName+a+"_treasure.txt";
std::vector<Treasure> *Treasures=s->getTreasures();
createFolder(filePath);// 获取目录路径
ofstream outFile(filePath);// 打开文件，如果文件不存在则创建
if (!outFile) {
    cerr << "无法打开文件: " << filePath << endl;
    return;
}
int i;
for(i=0;i<Treasures->size();i++){
Treasure *p=&(*Treasures)[i];;
// 将玩家信息写入一行，使用空格分隔
outFile<< p->GetSymbol()<<" "
<<p->Getpoint().Getx()<<" "
<< p->Getpoint().Gety() << "\n";
}
outFile.close(); // 关闭文件
}



void showMessage(const string& m){
    std::cout<<m<<std::endl;
    sleep(3);
    
}






void  FileSave:: readPlayer(const string& i){
string filePath=userPath+userName+i+"_base.txt";
ifstream inFile(filePath); // 打开文件进行读取
if (!inFile) {
    showMessage("无法打开用户文件");
    return;
}

float crit, evade;
std::string name, symbol,Wonpenname,Armorname;
int attack, hp, defend, x, y, golden, exp, level, attrPoint;

while (inFile >> crit >> evade >> name >> symbol >> attack >> hp >> defend
         >> x >> y >> golden>>exp >> level >> attrPoint>>Wonpenname>>Armorname) {

// 读取每个属性

// 检查是否成功读取到所有数据
if (inFile.fail()) {
    inFile.close(); // 关闭文件
    showMessage("读取文件数据失败");
    return;
}

}
inFile.close(); // 关闭文件
p->SetcritChance(crit);
p->SetdodgeChance(evade);
p->SetName(name);
p->SetSymbol(symbol);//继承模式改成组合模式
p->SetAttack(attack);
p->SetHP(hp);
p->SetDefind(defend);
Point point;
point.Setx(x);
point.Sety(y);
p->Setpoint(point);
p->SetGloden(golden);
p->SetExp(exp);
p->SetLevel(level);
p->SetAttrPoint(attrPoint);
//p->getArmor()==nullptr;
//p->getWeapon()==nullptr;
showMessage("读取用户信息成功");
}




void  FileSave:: readMonster(const string& a)
{
string filePath=monsterPath+userName+a+"_monster.txt";
std::vector<Monster> *Monsters=s->getMonsters();
std::vector<blame1> newMonsters;
ifstream inFile(filePath);
if (!inFile) {
    showMessage("无法打开怪兽文件");
    return;
}
Monsters->clear();
float crit, evade;
string name, symbol;
int attack, hp, defind, x, y, golden, exp, level, attrPoint;
while (inFile >> evade >>crit>> name >>hp>>attack>>defind>>symbol>>golden >>x >>y >>exp) {
// 读取每个属性
// 检查是否成功读取到所有数据
if (inFile.fail()) {
    inFile.close(); // 关闭文件
    showMessage("读取怪兽文件数据失败");
    return;
}
Point p;
p.Setx(x);
p.Sety(y);
blame1  m(p);
m.SetdodgeChance(evade);
m.SetcritChance(crit);
m.SetName(name);
m.SetHP(hp);
m.SetAttack(attack);
m.SetDefind(defind);
m.SetSymbol(symbol);
m.SetExp(exp);
m.SetGloden(golden);
newMonsters.push_back(m);

}
inFile.close();
int i;
for(i=0;i<newMonsters.size();i++){
blame1 b1 =newMonsters[i];
Monsters->push_back(b1);
}
showMessage("读取怪兽信息成功");
}

//读背包
void FileSave::readBag(const string& i)
{
string filePath=bagPath+userName+i+"_bag.txt";
WeaponFactory w;
ArmorFactory a;
PotionFactory y;
// std::vector<PropPtr> *bag=&p->Getbag();
ifstream inFile(filePath);
if (!inFile) {
    showMessage("无法打开背包文件");
    return;
}
p->Getbag().clear();
std::string name;
while(inFile>>name)
{
// 读取每个属性
// 检查是否成功读取到所有数据
if (inFile.fail()) {
    inFile.close(); // 关闭文件
    showMessage("读取背包文件数据失败");
    return;
}
if(name=="钻石")
{
Diamond d(name,32,"钻石");
p->Getbag().push_back(std::make_shared<Diamond>(d));
}
else 
    p->Getbag().push_back(AllFactory(name));

}
inFile.close();

}



void  FileSave:: readTreasure(const string& a){
string filePath=treasurePath+userName+a+"_treasure.txt";
std::vector<Treasure> *Treasures=s->getTreasures();
std::vector<Treasure> newTreasures;
ifstream inFile(filePath);// 打开文件，如果文件不存在则创建
if (!inFile) {
    showMessage("无法打开宝箱文件");
    return;
}
string name, symbol;
int attack, hp, defind, x, y, golden, exp, level, attrPoint;
while (inFile >> symbol >>x>> y) {
// 读取每个属性
// 检查是否成功读取到所有数据
if (inFile.fail()) {
    inFile.close(); // 关闭文件
    showMessage("读取宝箱文件数据失败");
    return;
}
Treasure t;
Point p;
p.Setx(x);
p.Sety(y);
t.SetSymbol(symbol);
t.Setpoint(p);
newTreasures.push_back(t);
}

inFile.close(); // 关闭文件
int i;
Treasures->clear();
for(i=0;i<newTreasures.size();i++){
Treasure t =newTreasures[i];
Treasures->push_back(t);
}
showMessage("读取宝箱信息成功");
}